package com.university.report.user.handler;

/**
 * @ClassName health_report
 * @Author Jinondo
 * @Date 2021/6/20 9:17
 */

import com.university.report.common.utils.JSONAuthentication;
import com.university.report.common.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 退出Handler
 */
@Component("myLogoutHandler")
@Slf4j
public class MyLogoutHandler extends JSONAuthentication implements LogoutHandler {

    @Autowired
    RedisTemplate redisTemplate2;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String jwtToken = request.getHeader("token");
        log.info("logout header Token = " + jwtToken);
        log.info("logout request getMethod = " + request.getMethod());
        //
        if (jwtToken!=null && !"".equals(jwtToken)) {
            String accesskey = JwtUtil.ACCESS_TOKEN + ":" + jwtToken;
            redisTemplate2.delete(accesskey);
            log.info("authentication = " + authentication);
            SecurityContextHolder.clearContext();
        }
    }
}
